package com.zp.configuration;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 避免前端精度丢失配置
 */
@Configuration
public class JacksonConfiguration {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(Long.class, ToStringSerializer.instance) // 把 Long 类型序列化为 String(默认实现了, 也可以从配置文件开启)
                    .deserializerByType(Long.class, new StringToLongDeserializer()) // 把 String 类型反序列化为 Long
                    .deserializerByType(Long.TYPE, new StringToLongDeserializer()); // 把 String 类型反序列化为 long
        };
    }
}